<?php

class TotalPriceBehavior extends CActiveRecordBehavior{

	public function getTotal() {
		$owner = $this->getOwner();
		if (!empty($owner->total_price)) return $owner->total_price;
		if (get_class($owner) == 'OrderHotel') {
			$owner->total_price = $this->totalPrice(CHtml::value($owner, 'addon.arrive_time'),CHtml::value($owner, 'addon.leave_time'));
		} else {
			$owner->total_price = CHtml::value($owner, 'items.item_price')*CHtml::value($owner, 'items.item_num');
		}
		$owner->updateByPk($owner->id, array('total_price'=>$owner->total_price));
		return $owner->total_price;
	}
	
	/**
	 * 根据时间计算总价
	 * Enter description here ...
	 * @param unknown_type $time1
	 * @param unknown_type $time2
	 */
	public function totalPrice($time1 = null, $time2 = null) {
		$owner = $this->getOwner();
		if (empty($time1)) return CHtml::value($owner, 'items.item_price')*CHtml::value($owner, 'items.item_num');
		$goods = CHtml::value($owner, 'items.goods');
		$price = 0;
		$time2 = empty($time2) ? $time1 : $time2;
		$diffTime = strtotime($time2) - strtotime($time1);
		$days = floor($diffTime / (3600 * 24))-1 < 0 ? 0 : floor($diffTime / (3600 * 24))-1;
		
		for ($i = 0; $i<=$days; $i++) {
			$price += $goods->timePriceObjAttr('price_dealer', date('Y-m-d', strtotime("+$i day", strtotime($time1)))) * CHtml::value($owner, 'items.item_num');
		}
		return $price;
	}
}

?>